# Figure 3: Effect of Ordered Net Benefits on Turnout


# 1. Load Packages ----

library(tidyverse)
library(lfe)
library(stargazer)
library(latex2exp)


# 2. Read in Data ----

load(file = "df_voxit_individual_analysis.RData")

# 3. Regressions ----

# reg1
lm.main1 <- felm(turnout ~ utility_prop_1st +
                   male + married + age + uni + knowledge + leftright | 
                   canton_id + datum_merge | 0 | canton_id + datum_merge, 
                 data = voxit_individual[voxit_individual$prop_voxit==1 ,],
                 psdef = F)
summary(lm.main1)

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==1]))

# reg2
lm.main2 <- felm(turnout ~ utility_prop_1st + utility_prop_2nd + 
                   male + married + age + uni + knowledge + leftright| 
                   canton_id + datum_merge | 0 | canton_id + datum_merge, 
                 data = voxit_individual[voxit_individual$prop_voxit==2 ,],
                 psdef = F)
summary(lm.main2)

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==2]))


# reg3
lm.main3 <- felm(turnout ~ utility_prop_1st + utility_prop_2nd + utility_prop_3rd +
                   male + married + age + uni + knowledge + leftright | 
                   canton_id + datum_merge | 0 | canton_id + datum_merge, 
                 data = voxit_individual[voxit_individual$prop_voxit==3 ,],
                 psdef = F)
summary(lm.main3)

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==3]))

# reg4
lm.main4 <- felm(turnout ~ utility_prop_1st + utility_prop_2nd + utility_prop_3rd +
                   utility_prop_4th +
                   male + married + age + uni + knowledge + leftright| 
                   canton_id + datum_merge | 0 | canton_id + datum_merge, 
                 data = voxit_individual[voxit_individual$prop_voxit==4 ,],
                 psdef = F)
summary(lm.main4)

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==4]))

# reg5
lm.main5 <- felm(turnout ~ utility_prop_1st + utility_prop_2nd + utility_prop_3rd +
                   utility_prop_4th + utility_prop_5th +
                   male + married + age + uni + knowledge + leftright| 
                   canton_id + datum_merge | 0 | canton_id + datum_merge , 
                 data = voxit_individual[voxit_individual$prop_voxit==5 ,],
                 psdef = F)
summary(lm.main5) # includes 11 voting days

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==5]))

# reg6
lm.main6 <- felm(turnout ~ utility_prop_1st + utility_prop_2nd + utility_prop_3rd +
                   utility_prop_4th + utility_prop_5th +  utility_prop_6th +
                   male + married + age + uni + knowledge + leftright | 
                   canton_id + datum_merge | 0 | canton_id , # not clustered on day level
                 data = voxit_individual[voxit_individual$prop_voxit==6,],
                 psdef = F)
summary(lm.main6) # includes only 2 voting days

# Voting Days 
length(unique(voxit_individual$datum_merge[voxit_individual$prop_voxit==6]))

# 4. Create Graph ----

# Generate a tidy data frame of regression results from six models
m <- list()
ordered_vars <- c("utility_prop_1st","utility_prop_2nd","utility_prop_3rd",
                  paste0("utility_prop_",c(4),"th"),
                  paste0("utility_prop_",c(5),"th"),
                  paste0("utility_prop_",c(6),"th"))

m_df <- tibble()

for (i in 1:6) {
  m[[i]] <- get(paste0("lm.main",i)) %>%
    broom::tidy() %>%
    filter(term %in% ordered_vars)%>%
    mutate(model = paste(i, "Propositions"))
  m_df <- m_df %>% bind_rows(m[[i]])
}
m_df$model[m_df$model=="1 Propositions"] <- "1 Proposition"
m_df$term_fct <- factor(m_df$term, levels = c( "utility_prop_6th", "utility_prop_5th",
                                               "utility_prop_4th", "utility_prop_3rd",
                                               "utility_prop_2nd", "utility_prop_1st"),
                        labels = c("6th","5th","4th","3rd","2nd","1st"))

# Plot the Coefficients
m_df %>%
  ggplot(data = ., 
         mapping = aes(x=estimate,y=term_fct,
                       xmin = estimate-1.96*std.error,
                       xmax = estimate+1.96*std.error)) +
  geom_point() +
  geom_errorbar(width = 0.2) +
  facet_wrap(~model) +
  theme_bw() +
  scale_y_discrete(name = "Ordered propositions (according to the net benefit)",
                   breaks = c("6th","5th","4th","3rd","2nd","1st"),
                   labels=unname(TeX(c('$U^{6th}$', '$U^{5th}$',
                                       '$U^{4th}$',  '$U^{3rd}$',
                                       '$U^{2nd}$', '$U^{1st}$')))) +
  scale_x_continuous(name = "Effect on turnout") +
  geom_vline(xintercept = 0, color = "red", linetype = "longdash")


# 5. Save Graph ----

ggsave(filename = "Figure3.pdf", height = 3.99, width = 7.8)




